package com.af.monitorModule.generator.service.impl;

import com.af.monitorModule.common.ResultEnum;
import com.af.monitorModule.generator.entity.ServerPush;
import com.af.monitorModule.generator.entity.VO.ServerVO;
import com.af.monitorModule.generator.mapper.ServerPushMapper;
import com.af.monitorModule.generator.service.ServerPushService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyz_4dou
 * @since 2021-02-27
 */
@Service
public class ServerPushServiceImpl extends ServiceImpl<ServerPushMapper, ServerPush> implements ServerPushService {

    @Autowired
    private ServerPushMapper serverPushMapper;

    @Override
    public JSONObject getShowLine(String serverId, String hours) {
        int hour = hours == null ? 1: Integer.parseInt(hours);
        QueryWrapper<ServerPush> wrapper = new QueryWrapper<>();
        wrapper.eq("server_id",serverId);
        wrapper.ge("push_date", LocalDateTime.now().plusHours(hour * -1));
        wrapper.orderByAsc("push_date");
        List<ServerPush> serverPushes = serverPushMapper.selectList(wrapper);
        JSONObject result = ResultEnum.OK.getResult();
        result.put("data",serverPushes);
        return result;
    }

    @Override
    public JSONObject getAll() {
        List<ServerVO> all = serverPushMapper.getAll();
        JSONObject res = ResultEnum.OK.getResult();
        res.put("data",all);
        return res;
    }
}
